package com.example.yls.sharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {
    private Button btnSave;
    private Button btnRead;
    private EditText edtName;
    private EditText edtAge;
    private RadioButton radioMan;
    private RadioButton radioWoman;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSave=findViewById(R.id.btn_save);
        btnRead=findViewById(R.id.btn_read);
        edtName=findViewById(R.id.edt_name);
        edtAge=findViewById(R.id.edt_age);
        radioMan=findViewById(R.id.radio_man);
        radioWoman=findViewById(R.id.radio_woman);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp=getSharedPreferences("mysp",
                        Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("isBoy",true);
                editor.putString("name","lisa");
                editor.putInt("age",20);
                editor.commit();
                Log.e("MainActivity","commit");
            }
        });

        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp=getSharedPreferences("mysp",
                        Context.MODE_PRIVATE);
                boolean isBoy=sp.getBoolean("isBoy",false);
                String name=sp.getString("name",null);
                int age=sp.getInt("age",0);
                Log.e("MainActivity","read isBoy = "+isBoy+" name = "+name+
                        " age = "+age);
            }
        });
    }
    @Override
    protected void onStop(){
        String name=edtName.getText().toString();
        int age=Integer.parseInt(edtAge.getText().toString());
        boolean isBoy=radioMan.isChecked();
        saveToSP(name,age,isBoy);
        super.onStop();
    }

    private void saveToSP(String name, int age, boolean isBoy) {
        SharedPreferences sp=getSharedPreferences("person", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isBoy",isBoy);
        editor.putString("name",name);
        editor.putInt("age",age);
        editor.commit();

    }

    @Override
    protected void onResume(){
        super.onResume();
        SharedPreferences sp=getSharedPreferences("person", Context.MODE_PRIVATE);
        boolean isBoy=sp.getBoolean("isBoy",false);
        String name=sp.getString("name",null);
        int age=sp.getInt("age",0);
        edtName.setText(name);
        edtAge.setText(String.valueOf(age));
        radioMan.setChecked(isBoy);
        radioWoman.setChecked(!isBoy);
    }
}
